package com.zsix.system.strategy.userByTaskRule;

import com.zsix.common.util.StringUtils;
import com.zsix.system.mapper.taskAssigneeMapper.ApprovalTrackMapper;
import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.taskAssignee.ApprovalTrack;
import com.zsix.common.entity.taskAssignee.AssigneeUserEntity;
import com.zsix.common.util.RedisUtils;
import com.zsix.common.util.SystemConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 按照指定节点的处理人，多人处理
 */
@Component
public class ByNodeList implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private ApprovalTrackMapper approvalTrackMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //当前登陆人信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        //查找绑定的接收人
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeUserEntity> assigneeUserEntityList = (List<AssigneeUserEntity>) hashMap.get("assigneeUserEntityList");
        String nextWorkEmp = "";
        String manager="";
        List<String> byTaskLeaderList=new ArrayList<>();
        for (AssigneeUserEntity ae : assigneeUserEntityList) {
            String sender="";
            List<ApprovalTrack> approvalTrackList=approvalTrackMapper.getUserTaskTrack(procInstId);
            if(approvalTrackList.size()>0){
                for (ApprovalTrack approvalTrack:approvalTrackList){
                    if(ae.getUsername().equals(approvalTrack.getUserTaskId())) {
                        sender = approvalTrack.getUserName();
                        if(StringUtils.isNull(sender))
                            manager=username;
                        else
                            manager=sender;

                        if(nextWorkEmp.contains(manager+","))
                            continue;
                        byTaskLeaderList.add(manager);
                        nextWorkEmp += manager + ",";
                    }
                }

            }
            else{
                manager=username;
                byTaskLeaderList.add(manager);
                nextWorkEmp=manager;
            }
        }

        //放入缓存，用于发送消息推送
        variables.put(taskid + "_byTaskLeaderList", byTaskLeaderList);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "byNodeList";
    }
}
